$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;

    //点击返回箭头 清除 缓存 打开 storekeeper.html
    $(".head .goBack").on("click", function () {
        window.location.href = "storekeeper.html";
    });

    $(".head .icon-lishi1").on("click", function () {
        window.location.href = "pt_cgrk_lishi.html";
    });

    //点击 刷新 按钮  重新加载页面
    $(".page .head").on("click", "span.icon-refresh-1-copy", function () {
        window.location.href = "pt_cgrk.html";
    });

    $("#goods_id").focus();

    //输入车牌号
    $("#goods_id").on("keyup", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) return;
            pass = 0
            var code = $(this).val();
            if (!code) {
                pass = 1;
                return $alert("车牌号不可为空");
            }
            getGaugeTickets(code);
        }
    });

    function getGaugeTickets(carNum) {
        //加载提示符 显示
        $(".content .list-block ul#work_list .infinite-scroll-preloader").show();
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/wirerodpurchaseinorder/getGaugeTickets',
            dataType: 'json',
            data: {
                carNum: carNum
            },
            json: 'callback',
            cache: false,
            timeout: 60000,
            success: function (res) {
                pass = 1;
                if (res.success) {
                    var data = res.result;
                    //清空页面中的单子
                    $("#work_list").find("li").remove();
                    //隐藏提示符 显示
                    $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                    //console.log(data);
                    var data_html = '';
                    for (var i = 0; i < data.length; i++) {
                        data_html += '<li>' +
                            '<p class="li_title">运单号：<span class="sp work_bill_no">' + data[i].bill_no + '</span><span class="icon iconfont icon-right pull-right"></span></p>' +
                            '<div class="row no-gutter li_content"><div class="col-100">' +
                            '<p class="p_none"><span>id:</span><span class="sp goods_id">' + data[i]._id + '</span></p>' +
                            '<p class="p_none"><span>id:</span><span class="sp goods_re_ware_code">' + data[i].re_ware_code + '</span></p>' +
                            '<div class="details">' +
                            '<p><span>磅单号:</span><span class="sp goods_weighing_no">' + (data[i].weighing_no) + '</span></p>' +
                            '<p><span>车牌:</span><span class="sp goods_plate_no">' + (data[i].plate_no ? data[i].plate_no : "") + '</span>' +
                            '<span>&nbsp;&nbsp;司机:</span><span class="sp goods_driver_name">' + (data[i].driver_name ? data[i].driver_name : "") + '</span></p>' +
                            '<p><span>物料编码:</span><span class="sp goods_inv_code">' + data[i].inv_code + '</span></p>' +
                            '<p><span>物料名称:</span><span class="sp goods_inv_name">' + data[i].inv_name + '</span>' +
                            '<p><span>物料型号:</span><span class="sp goods_inv_model">' + (data[i].inv_model || "") + '</span>' +
                            '<span>规格:</span><span class="sp goods_inv_spec">' + (data[i].inv_spec || "") + '</span></p>' +
                            '<p><span>总重量:</span><span class="sp goods_actual_net_weight">' + (data[i].actual_net_weight) + '</span>吨' +
                            '<span>&nbsp;&nbsp;</span><span class="sp goods_piece_excuted">' + (data[i].piece_excuted) + '</span>件</p>' +
                            '<p><span>收货仓库:</span><span class="sp goods_re_ware_name">' + data[i].re_ware_name + '</span></p>' +
                            '<p><span>创建时间:</span><span class="sp goods_create_time">' + data[i].create_time + '</span></p>';
                        if (data[i].comment) {
                            data_html += '<p><span>备注:</span><span class="sp comment" style="display: inline;">' + data[i].comment + '</span></p>';
                        }
                        data_html += '</div></div></div></li>';
                    }
                    $("#work_list").append(data_html);
                    //改变 共计 数
                    $(".content div.ul_foot span.sp_js").html($("#work_list li").length);
                    $("#goods_id").val("");
                } else {
                    //隐藏提示符 显示
                    $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                    $alert(res.message);
                    setTimeout(function () {
                        $("#goods_id").val("").focus();
                    }, 1400);
                }
            },
            error: function (error) {
                //隐藏提示符 显示
                pass = 1;
                $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                if (error.status == 0) {
                    //loading_hide();
                    $alert("请求超时，请重新操作！");
                } else {
                    //loading_hide();
                    $alert(error.responseText);
                }
            }
        });
    }

    //点击 箭头
    $(".content #work_list").on("click", "li .li_title", function () {
        var id = $(this).parent().find("span.goods_id").html();
        var bill_no = $(this).parent().find("span.work_bill_no").html();
        var ware_name = $(this).parent().find("span.goods_re_ware_name").html();
        var ware_code = $(this).parent().find("span.goods_re_ware_code").html();
        var plate_no = $(this).parent().find("span.goods_plate_no").html();
        var inv_code = $(this).parent().find("span.goods_inv_code").html();
        var inv_name = $(this).parent().find("span.goods_inv_name").html();
        var inv_model = $(this).parent().find("span.goods_inv_model").html();
        var inv_spec = $(this).parent().find("span.goods_inv_spec").html();
        var actual_net_weight = $(this).parent().find("span.goods_actual_net_weight").html();
        var piece_excuted = $(this).parent().find("span.goods_piece_excuted").html();
        var work = {
            id: id,
            bill_no: bill_no,
            ware_name: ware_name,
            ware_code: ware_code,
            plate_no: plate_no,
            inv_code: inv_code,
            inv_name: inv_name,
            inv_model: inv_model,
            inv_spec: inv_spec,
            actual_net_weight: actual_net_weight,
            piece_excuted: piece_excuted
        };
        localStorage.setItem("work", JSON.stringify(work));
        window.location.href = "pt_cgrk_one.html";
    });

});